package com.itheima.controller;

import com.itheima.utils.AliyunOSSUtils;
import com.itheima.utils.AliyunOssProp;
import com.itheima.utils.Result;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/*文件*/
@RestController
@Slf4j
public class UploadController {

    @Autowired
    private AliyunOssProp aliyun;
/*    @Value("${aliyun.oss.endPoint}")
    private String endPoint ;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName ;*/
/*
    @SneakyThrows
    public Result uploadFile (MultipartFile file){
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String suffix = filename.substring(filename.lastIndexOf("."));
        log.info("uuid:{} , suffix:{}",uuid,suffix);
        file.transferTo(new File("E:\\TuPian\\"+uuid+suffix) );
        return Result.success();
    }*/

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @SneakyThrows
    public Result uploadFile(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        byte[] fileBytes = file.getBytes();
        String url = AliyunOSSUtils.upload(aliyun.getEndPoint(), aliyun.getBucketName(), fileBytes, suffix);
        return Result.success(url);
    }

}
